//
//  BLHotelDetailViewController.m
//  blanket
//
//  Created by Mac on 2018/11/14.
//  Copyright © 2018 stoneobs@icloud.com. All rights reserved.
//

#import "BLHotelDetailViewController.h"
#import "BLHotelDetailModel.h"
#import "BLDatePresentViewController.h"
#import "BLHotelCommitViewController.h"
#import "BLRoomModel.h"
#import "BLHotleRoomTableViewCell.h"
#import "BLRoomDetailViewController.h"
@interface BLHotelDetailViewController ()
@property(nonatomic, strong) BLHotelDetailModel                     *model;/**< 详情mdoel */
@property(nonatomic, strong) STAdvertingScrollView                     *adverView;/**< <##> */
@property(nonatomic, strong) STLabel                        *titleLable;
@property(nonatomic, strong) STLabel                         *priceLable;//积分商城隐藏，采集商城展示
@property(nonatomic, strong) STLabel                         *desLable;
@property(nonatomic, strong) NSMutableArray                     *dataSouce;/**< <##> */
@end

@implementation BLHotelDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"铺盖酒店";
    [self sendHotleDetailRequest];
    __weak typeof(self) weakSelf =  self;

    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf sendHotleListRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    header.backgroundColor = [UIColor whiteColor];
    
    self.adverView = [[STAdvertingScrollView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 275) andWithArray:@[@"banner1",@"banner1",@"banner1",@"banner1",@"banner1"] handle:^(NSInteger num) {
        
    }];
    self.adverView.pageControll.bottom = self.adverView.height - 15;
    self.adverView.pageControll.currentPageIndicatorTintColor = [UIColor whiteColor];
    self.adverView.pageControll.pageIndicatorTintColor = KL_BlueBackGroundColor;
    self.adverView.pageControll.centerX = UIScreenWidth / 2;
    self.adverView.canAutoScroll = YES;
    self.adverView.time = 5;
    [header addSubview:self.adverView];
    self.adverView.imageUrlsArray = self.model.imgs;
    
    self.titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, self.adverView.bottom +10, UIScreenWidth - 20, 40)
                                                text:self.model.hotel_name
                                           textColor:BL_secendTextColor
                                                font:14
                                         isSizetoFit:NO
                                       textAlignment:NSTextAlignmentLeft];
    self.titleLable.numberOfLines = 0;
    [header addSubview:self.titleLable];
    
    self.priceLable = [[STLabel alloc] initWithFrame:CGRectMake(0 + 10, self.titleLable.bottom, UIScreenWidth - 20 , 20)
                                                text:@"价格:￥456"
                                           textColor:BL_redColor
                                                font:14
                                         isSizetoFit:NO
                                       textAlignment:NSTextAlignmentLeft];
    [header  addSubview:self.priceLable];
    self.priceLable.text = [NSString stringWithFormat:@"价格:￥%@",_model.price];
    self.priceLable.attributedText = [self.priceLable.text st_convertAttributeStringWithKeyWord:@"价格:" attributes:@{NSForegroundColorAttributeName:BL_secendTextColor}];
    
    
    self.desLable = [[STLabel alloc] initWithFrame:CGRectMake( 10, self.priceLable.bottom + 10, UIScreenWidth - 20 - 0, 20)
                                              text:[NSString stringWithFormat:@"酒店简介:%@",self.model.brief]
                                         textColor:BL_secendTextColor
                                              font:14
                                       isSizetoFit:NO
                                     textAlignment:NSTextAlignmentLeft];
    [header addSubview:self.desLable];
    [NSString lableAutoAdjustheightWithLabel:self.desLable];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, self.desLable.bottom + 10, UIScreenWidth - 20, 20)
                                                     text:[NSString stringWithFormat:@"酒店电话:%@",self.model.phone]
                                                textColor:BL_secendTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:titleLable];
    
    [NSString lableAutoAdjustheightWithLabel:titleLable];
    
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, titleLable.bottom + 20, UIScreenWidth, 10)];
    line.backgroundColor = self.tableView.backgroundColor;
    [header addSubview:line];
    
    
    header.height = line.bottom;
    self.tableView.tableHeaderView = header;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return BLHotleRoomTableViewCell.cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    BLHotleRoomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[BLHotleRoomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    BLRoomModel * model = self.dataSouce[indexPath.row];
    cell.model = model;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BLRoomModel * model = self.dataSouce[indexPath.row];
    BLRoomDetailViewController * vc = BLRoomDetailViewController.new;
    vc.model = model;
    vc.detailModel = self.model;
    [self.navigationController pushViewController:vc animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --NetWork Method
- (void)sendHotleDetailRequest{
    //    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"/api/hotel/hotel"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if (self.hid.length) {
        [dic setObject:self.hid forKey:@"id"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {

                                                 self.model = [BLHotelDetailModel mj_objectWithKeyValues:responseObject[@"data"]];
                                                 self.model.price = self.money;
                                                 [self configSubView];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {

                                             }];
}
- (void)sendHotleListRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"/api/hotel/roomList/p/1"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if (self.hid.length) {
        [dic setObject:self.hid forKey:@"id"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 NSArray * array = responseObject[@"data"];
                                                 self.dataSouce = NSMutableArray.new;
                                                 for (NSDictionary * modelDIc in array) {
                                                     BLRoomModel * model = [BLRoomModel mj_objectWithKeyValues:modelDIc];
                                                     [self.dataSouce addObject:model];
                                                 }
                                                 [self.tableView reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
@end
